<?php
namespace Common\Controller;
use Common\Controller\CommonController;

class GuessFatherSongController extends CommonController
{

    /**
     * 缓存歌曲题库
     */
    public function cachequesion($time){
        $song_data      =  M('fathersong_questions')->select();
        $song_list      =   array();
        $number_1       =   array();
        $number_2       =   array();
        $number_3       =   array();
        $number_4       =   array();
        $number_5       =   array();
        if (! empty($song_data))
        {
            foreach ($song_data as $key=>$value)
            {
                $value['audio'] = $this->url.$value['audio'];
                $value['song'] = $this->url.$value['song'];
                $value['picture'] = $this->url.$value['picture'];

                $song_list[$value['id']]    =   $value;

                switch ($value['number'])
                {
                    case 1:
                        $number_1[] =   $value;
                    break;

                    case 2:
                        $number_2[] =   $value;
                        break;

                    case 3:
                        $number_3[] =   $value;
                        break;

                    case 4:
                        $number_4[] =   $value;
                        break;

                    case 5:
                        $number_5[] =   $value;
                        break;
                }
            }
        }
        S('fathersong_questions',$song_list,$time);
        S('fathersong_questions1',$number_1,$time);
        S('fathersong_questions2',$number_2,$time);
        S('fathersong_questions3',$number_3,$time);
        S('fathersong_questions4',$number_4,$time);
        S('fathersong_questions5',$number_5,$time);
    }

    /**
     *
     * 读取歌曲题库缓存并返回随机一个值
     * @return mixed
     */
    public function getcachequersion(){

        $val = S('fathersong_questions');

        if (!$val||$val==""){
            $this->cachequesion(3600*24*30);
            $val = S('fathersong_questions');
        }
        srand((float) microtime() * 10000000);
        $id = array_rand($val);

         return $val[$id];
    }

    /**
     *
     * 读取歌曲题库缓存并返回随机值
     * @return mixed
     */
    public function getmanyquersion($uid,$num = 5){
        
        $val =  S('fathersong_questions');

        if (!$val||$val==""){
            $this->cachequesion(3600*24*30);
            $val = S('fathersong_questions');
        }

        //答对的做验证
         $where_record           =   array();
         $field                  =   array('number_1','number_2','number_3','number_4','number_5');
         $where_record['uid']    =   $uid;
         $where_record['status'] =   1;
         $questions_record       =   M('fathersong_questions_record')->where($where_record)->field($field)->find();

        $used_record            =   array();
        if (! empty($questions_record))
        {
            for ($m=1; $m<=5; $m++)
            {
                $used_record[]  =   $questions_record['number_'.$m];
            }
        }

        $data           =   array();
        for ($x=1; $x<=5; $x++)
        {
            $number_data    =   S('fathersong_questions'.$x);
            if (! empty($number_data))
            {
                $length     =   count($number_data) - 1;
                $index      =   rand(0,$length);

                if (! empty($used_record))
                {
                    //过滤掉最近做过的题目
                    $aswer_id  =   $number_data[$index]['aswer_id'];

                    while (in_array($aswer_id, $used_record))
                    {
                        $index      =   rand(0,$length);
                        $aswer_id   =   $number_data[$index]['aswer_id'];
                    }
                }
                else
                {
                    $number_data[$index]['option']  =   json_decode($number_data[$index]['option'], true);
                    $data[]     =   $number_data[$index];
                }
            }
        }

        if (! empty($data))
        {
            $this->set_question_record($uid,$data);
        }
        return $data;
    }

    /**
     * 生成红包
     */
    public function create_redbag($uid)
    {
        $where_relationship                 =   array();
        $field                              =   array('id');
        $where_relationship['my_member_id'] =   $uid;
        $is_shere                           =   M('fathersong_relationship')->where($where_relationship)->field($field)->find();

        $where_red                  =   array();
        $where_red['member_id']     =   $uid;
        $redbag_num                 =   M('fathersong_redbag')->where($where_red)->count();
        if (!empty($redbag_num))
        {
            $red_total                  =   $is_shere ? rand(10,15) / 100  :  rand(10,13) / 100; //奖金
        }
        else
        {
            $red_total                  =   0.88; //奖金
        }

        $insert_data                =   array();
        $insert_data['member_id']   =   $uid;
        $insert_data['red_total']   =   number_format($red_total, 2);
        $insert_data['status']      =   0;
        $insert_data['create_time'] =   date('Y-m-d H:i:s');
        $insert_data['total_sn']    =   $this->create_order();
        $insert_data['time']        =   time();

        $result     =    M('fathersong_redbag')->add($insert_data);
        if (! empty($result))
        {
            $insert_data['challenge_id']    =   $result;
            $this->create_redbag_log($insert_data);
            $this->create_change_log($insert_data);
        }
        return  $result;
    }

    //生成红包记录
    private function create_redbag_log($insert_data)
    {
        $insert_data['create_time'] =   date('Y-m-d H:i:s');
        $insert_data['time']        =   time();
        M('fathersong_redbag_log')->add($insert_data);
    }

    //生成收支记录
    private  function create_change_log($insert_data)
    {
        //添加一条收入记录
        $insert_change_log                  =   array();
        $insert_change_log['game_id']       =   1;
        $insert_change_log['member_id']     =   $insert_data['member_id'];
        $insert_change_log['red_total']     =   '+'.$insert_data['red_total'];
        $insert_change_log['status']        =   0;
        $insert_change_log['create_time']   =   $insert_data['create_time'];
        $insert_change_log['from_sn']       =   $insert_data['total_sn'];
        $insert_change_log['type_id']       =   1;
        $insert_change_log['status_text']   =   '现金红包';
        $insert_change_log['time']          =   time();
        M('fathersong_change_log')->add($insert_change_log);
    }

    /**
     * 生成订单
     */
    private function create_order()
    {
        $order_id           =   'HB'.date('YmdHis').rand(100000,999999);
        $where              =   array();
        $field              =   array('challenge_id');
        $where['total_sn']  =   $order_id;
        $resutl             =   M('fathersong_redbag')->where($where)->field($field)->find();
        if (empty($resutl))
        {
            return  $order_id;
        }
        else
        {
            $this->create_order();
        }
    }

    /**
     * 设置题目玩的记录
     */
    private function set_question_record($uid,$question)
    {
        //删除之前的记录
        $where_record           =   array();
        $where_record['uid']    =   $uid;
        M('fathersong_questions_record')->where($where_record)->delete(); // 删除所有状态为0的用户数据

        //添加新记录
        $insert_played              =   array();
        $insert_played_record       =   array();
        $insert_data                =   array();
        $insert_data['uid']         =   $uid;
        $insert_data['create_time'] =   time();

        $insert_played_record['uid']        =   $uid;
        $insert_played_record['create_time']=   time();
        $insert_played_record['type']       =   1;

        foreach ($question as $val)
        {
            $insert_data['number_'.$val['number']]    =     $val['aswer_id'];
            $insert_played_record['question_id']      =     $val['id'];

            $insert_played[]                          =     $insert_played_record;
        }
        $insert_data['number']      =   count($question);

        //记录当前的答案
        M('fathersong_questions_record')->add($insert_data);

        //记录玩过的记录
        if (! empty($insert_played))
        {
            M('fathersong_questions_played')->addAll($insert_played);
        }
    }

    /**
     *
     * 读取歌曲题库缓存并返回随机值
     * @return mixed
     */
    public function geta_ctivity_quersion($member_data,$type = 1,$num = 20)
    {
        $val =  S('fathersong_questions');

        if (!$val||$val==""){
            $this->cachequesion(3600*24*30);
            $val = S('fathersong_questions');
        }

        //答对的做验证
        //答对的做验证
        $where_record           =   array();
        $field                  =   array('question_id');
        $where_record['uid']    =   $member_data['uid'];
        $questions_record       =   M('fathersong_questions_played')->where($where_record)->field($field)->select();

        $used_record            =   array();
        if (! empty($questions_record))
        {
            foreach ($questions_record as $record_cal)
            {
                $used_record[]  =   $record_cal['question_id'];
            }
        }

        $total                  =   M('fathersong_questions')->count();
        //整理记录
        $same                   =   array_count_values($used_record);
        $same_num               =   count($same);
        if ( ($total - $same_num) < $num)
        {
            M('fathersong_questions_played')->where($where_record)->delete();
            $used_record        =   array();
        }

        $data                               =   array();
        $answer_list                        =   array();
        $id_list                            =   array();
        $insert_played                      =   array();
        $insert_played_record               =   array();
        $insert_played_record['uid']        =   $member_data['uid'];
        $insert_played_record['create_time']=   time();
        $insert_played_record['type']       =   1;

        $number_data    =   S('fathersong_questions');
        for ($x=1; $x<=$num; $x++)
        {
            if (! empty($number_data))
            {
                $length     =   count($number_data) - 1;
                $index      =   rand(0,$length);
                $id         =   $number_data[$index]['id'];

                if (in_array($id, $used_record) || in_array($id, $id_list) || empty($number_data[$index]))
                {
                    //递归去除重复
                    $index  =   self::random_question($used_record, $id_list);
                    $number_data[$index]['option']            =   json_decode($number_data[$index]['option'], true);
                    $data[]     =   $number_data[$index];
                    $insert_played_record['question_id']      =     $number_data[$index]['id'];
                    $insert_played[]                          =     $insert_played_record;
                    $answer_list[]                            =     $number_data[$index]['aswer_id'];
                    $id_list[]                                =     $number_data[$index]['id'];
                }
                else
                {
                    $number_data[$index]['option']            =   json_decode($number_data[$index]['option'], true);
                    $data[]     =   $number_data[$index];
                    $insert_played_record['question_id']      =     $number_data[$index]['id'];
                    $insert_played[]                          =     $insert_played_record;
                    $answer_list[]                            =     $number_data[$index]['aswer_id'];
                    $id_list[]                                =     $number_data[$index]['id'];
                }
            }
        }

        //去空处理
        $insert_played  = array_filter($insert_played);
        $answer_list    = array_filter($answer_list);
        $data           = array_filter($data);
        $id_list        = array_filter($id_list);

        if (! empty($insert_played) && ! empty($answer_list))
        {
            M('fathersong_questions_played')->addAll($insert_played);

            $where_record           =   array();
            $where_record['uid']    =   $member_data['uid'];
            $where_record['status'] =   0;
            $field_record           =   array('id','create_time');
            $not_finish             =   M('fathersong_activity_record')->where($where_record)->field($field_record)->select();
            if (! empty($not_finish))
            {
                foreach ($not_finish as $not_data)
                {
                    $where                      =   array();
                    $where['id']                =   $not_data['id'];
                    $update_data                =   array();
                    $update_data['update_time'] =   time();
                    //$update_data['time']        =   time() - $not_data['create_time'];
                    $update_data['service_time']=   time() - $not_data['create_time'];
                    $update_data['status']      =   -1;
                    M('fathersong_activity_record')->where($where)->save($update_data);
                }
            }

            //写入活动记录 （newsong_activity_record）
            $insert_activity                =   array();
            $insert_activity['uid']         =   $member_data['uid'];
            $insert_activity['nickname']    =   $member_data['nickname'];
            $insert_activity['avatarurl']   =   $member_data['avatarurl'];
            $insert_activity['type']        =   $type;
            $insert_activity['number']      =   count($answer_list);
            $insert_activity['create_time'] =   time();
            $insert_activity['date']        =   date('Y-m-d');
            $insert_activity['answer']      =   implode("|",$answer_list);
            $insert_activity['same_num']    =   array_count_values($id_list);
            M('fathersong_activity_record')->add($insert_activity);
        }
        return $data;
    }

    //练习题目
    public function practice_question($uid, $num = 20)
    {
        $val =  S('fathersong_questions');

        if (!$val||$val==""){
            $this->cachequesion(3600*24*30);
            $val = S('fathersong_questions');
        }

        //答对的做验证
        //答对的做验证
        $where_record           =   array();
        $field                  =   array('question_id');
        $where_record['uid']    =   $uid;
        $questions_record       =   M('fathersong_questions_played')->where($where_record)->field($field)->select();

        $used_record            =   array();
        if (! empty($questions_record))
        {
            foreach ($questions_record as $record_cal)
            {
                $used_record[]  =   $record_cal['question_id'];
            }
        }

        $total                  =   M('fathersong_questions')->count();
        //整理记录
        $same                   =   array_count_values($used_record);
        $same_num               =   count($same);
        if ( ($total - $same_num) < $num)
        {
            M('fathersong_questions_played')->where($where_record)->delete();
            $used_record        =   array();
        }

        $data                               =   array();
        $answer_list                        =   array();
        $id_list                            =   array();
        $insert_played                      =   array();
        $insert_played_record               =   array();
        $insert_played_record['uid']        =   $uid;
        $insert_played_record['create_time']=   time();
        $insert_played_record['type']       =   1;

        $number_data    =   S('fathersong_questions');
        for ($x=1; $x<=$num; $x++)
        {
            if (! empty($number_data))
            {
                $length     =   count($number_data) - 1;
                $index      =   rand(0,$length);
                $id         =   $number_data[$index]['id'];

                if (in_array($id, $used_record) || in_array($id, $id_list) || empty($number_data[$index]))
                {
                    //递归去除重复
                    $index  =   self::random_question($used_record, $id_list);
                    $number_data[$index]['option']            =   json_decode($number_data[$index]['option'], true);
                    $data[]     =   $number_data[$index];
                    $insert_played_record['question_id']      =     $number_data[$index]['id'];
                    $insert_played[]                          =     $insert_played_record;
                    $answer_list[]                            =     $number_data[$index]['aswer_id'];
                    $id_list[]                                =     $number_data[$index]['id'];
                }
                else
                {
                    $number_data[$index]['option']            =   json_decode($number_data[$index]['option'], true);
                    $data[]     =   $number_data[$index];
                    $insert_played_record['question_id']      =     $number_data[$index]['id'];
                    $insert_played[]                          =     $insert_played_record;
                    $answer_list[]                            =     $number_data[$index]['aswer_id'];
                    $id_list[]                                =     $number_data[$index]['id'];
                }
            }
        }

        //去空处理
        $insert_played  = array_filter($insert_played);
        $answer_list    = array_filter($answer_list);
        $data           = array_filter($data);

        if (! empty($insert_played) && ! empty($answer_list))
        {
            M('fathersong_questions_played')->addAll($insert_played);
        }
        return $data;
    }

    //过滤相同题目的
    private function random_question($used_record, $id_list)
    {
        $number_data=   S('fathersong_questions');
        $length     =   count($number_data) - 1;
        $index      =   rand(0,$length);
        $id         =   $number_data[$index]['id'];
        if (in_array($id, $used_record) || in_array($id, $id_list) || empty($number_data[$index]))
        {
            return $this->random_question($used_record, $id_list);
        }
        else
        {
            return $index;
        }
    }

}